<?php

/**
 * This is the model class for table "tbl_businessModel".
 *
 * The followings are the available columns in table 'tbl_businessModel':
 * @property integer $id
 * @property string $customerSegment
 * @property string $scale
 * @property string $focus
 */
class BusinessModel extends CActiveRecord
{
	
	const CUSTOMERSEGMENT_MASSMARKET = 'Mass market';
    const CUSTOMERSEGMENT_NICHEMARKET = 'Niche market';
    const CUSTOMERSEGMENT_DIVERSIFIED = 'Diversified';    
        
    const SCALE_LOCAL = 'Local';
    const SCALE_GLOBAL = 'Global';
        
    const FOCUS_VERTICAL = 'Vertical';
    const FOCUS_HORIZONTAL = 'Horizontal';
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return BusinessModel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_businessModel';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('customerSegment, scale, focus', 'length', 'max'=>128),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, customerSegment, scale, focus', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(		
			'businessCases' => array(self::HAS_MANY, 'BusinessCase', 'businessModel_id'),
			'tblRevenuestreams' => array(self::MANY_MANY, 'Revenuestreams', 'tbl_businessModel_revenueStreams(businessModel_id, revenueStreams_id)'),
			'tblPartnership' => array(self::MANY_MANY, 'Partnership', 'tbl_businessModel_partnership(businessModel_id, partnership_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'customerSegment' => 'Customer Segment',
			'scale' => 'Scale',
			'focus' => 'Focus',
			'tblRevenuestreams' => 'Revenue Streams',
			'tblPartnership' => 'Partnership',
		
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('customerSegment',$this->customerSegment,true);
		$criteria->compare('scale',$this->scale,true);
		$criteria->compare('focus',$this->focus,true);

		$criteria->compare('tblRevenuestreams', $this['tblRevenuestreams']);
        $criteria->compare('tblPartnership', $this['tblPartnership']);
                
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
		
        
        /**
         * Retrieves a model based on the current search/filter conditions.
         * @return CActiveDataProvider the data provider that can return the model based on the search/filter conditions.
         */
        public function searchSameModel()
        {
                // Warning: Please modify the following code to remove attributes that
                // should not be searched.

                $criteria=new CDbCriteria;
                
                $criteria->compare('customerSegment',$this->customerSegment,true);
                $criteria->compare('scale',$this->scale,true);
                $criteria->compare('focus',$this->focus,true);

                $criteria->compare('tblRevenuestreams', $this['tblRevenuestreams']);
                $criteria->compare('tblPartnership', $this['tblPartnership']);
                
                return new CActiveDataProvider($this, array(
                        'criteria'=>$criteria,
                ));
        }
        
        public function getCases($id, $businessCaseArray){
                
                $businessCases = array();
                foreach ($businessCaseArray as $businessCase) {
                        if($businessCase->businessModel_id == $id){
                                        array_push($businessCases, $businessCase);
                        };
                }
                        
                $dataProvider = new CArrayDataProvider($businessCases, array(
                                'pagination'=>array(
                                                'pageSize'=>3,
                                                 ),
                                )); 
                return $dataProvider;
        }
        
        public function getCustomerSegmentOptions()
        {
                 return array(
                self::CUSTOMERSEGMENT_MASSMARKET=>'Mass market',
                self::CUSTOMERSEGMENT_NICHEMARKET=>'Niche market',
                self::CUSTOMERSEGMENT_DIVERSIFIED=>'Diversified',
                  );
        }      
       
        
        public function getScaleOptions()
        {
                 return array(
                self::SCALE_LOCAL=>'Local',
                self::SCALE_GLOBAL=>'Global',
                  );
        }
        
        public function getFocusOptions()
        {
                 return array(
                self::FOCUS_VERTICAL=>'Vertical',
                self::FOCUS_HORIZONTAL=>'Horizontal',
                  );
        }
        
        public function getForm() {
                return new BusinessModelForm(array(
                        'showErrorSummary'=>true,
                        'attributes'=>array('id'=>'bcase'),
                        'elements'=>array(                               
                                'customerSegment'=>array(
                                        'type'=>'dropdownlist',
                                        'items'=>$this->getCustomerSegmentOptions(),
                                        'layout'=>'{label}<br/>{input}<br/>{error}'
                                ),
                                'scale'=>array(
                                        'type'=>'dropdownlist',
                                        'items'=>$this->getScaleOptions(),
                                        'layout'=>'{label}<br/>{input}<br/>{error}'
                                ),
                                'focus'=>array(
                                        'type'=>'dropdownlist',
                                        'items'=>$this->getFocusOptions(),
                                        'layout'=>'{label}<br/>{input}<br/>{error}'
                                ),                              
                                'tblRevenuestreams'=>array(
                                        'type'=>'checkboxlist',
                                        'items'=>CHtml::listData(RevenueStreams::model()->findAll(), 'id', 'name'),
                                        'name'=>'revenueStreams',
                                        'attributeitem' => 'id',
                                        'separator'=>'<br>',
                                        'labelOptions'=>array('style'=>'display:inline;'),                                      
                                        'layout'=>'{label}<br/>{input}<br/>{error}'
                                ),                              
                                'tblPartnership'=>array(
                                        'type'=>'checkboxlist',
                                        'items'=>CHtml::listData(Partnership::model()->findAll(), 'id', 'type'),
                                        'name'=>'partnership',
                                        'attributeitem' => 'id',
                                        'separator'=>'<br>',
                                        'labelOptions'=>array('style'=>'display:inline;'),                                      
                                        'layout'=>'{label}<br/>{input}<br/>{error}'
                                ),
                        ),
                        'buttons'=>array(
                                'submit'=>array(
                                        'type'=>'submit',
                                        'label'=>'Finish'
                                )
                        )
                ), $this);
        }
        
        public function isRecordExists($businessModel)
        {               
        	/*
                $sqlBusinessModel = 'SELECT tbl_businessModel.id
                        FROM tbl_businessModel
                        WHERE tbl_businessModel.revenueModel = :revenueModel AND
                        tbl_businessModel.customerSegment = :customerSegment AND
                        tbl_businessModel.partnership = :partnership AND
                        tbl_businessModel.scale = :scale AND
                        tbl_businessModel.focus = :focus' ;
                                        
                $dataBusinessModel=new CSqlDataProvider($sqlBusinessModel, array(
                'params'=>array(':revenueModel'=>$businessModel->revenueModel,
                                                ':customerSegment'=>$businessModel->customerSegment,
                                                ':partnership'=>$businessModel->partnership,
                                                ':scale'=>$businessModel->scale,
                                                        ':focus'=>$businessModel->focus),
                'sort'=>array(
                        'attributes'=>array(
                         'id'),
                          ),
                'pagination'=>array(
                'pageSize'=>10,
                ),
                        ));
                        
                $dataBusinessModel = $dataBusinessModel->getData();
                */
                //return $dataBusinessModel[0]['id'];   
                return null;                           
                
        }
	
}